package com.ciko.code.utils;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.UUID;


public class DeviceUtil
{
    private Integer simId_1 = 0;
    private Integer simId_2 = 1;
    private String  imsi_1  = "";
    private String  imsi_2  = "";
    private String  imei_1  = "";
    private String  imei_2  = "";
    private Context mContext;

    public DeviceUtil(Context mContext) {
        this.mContext = mContext;
    }

    /** 获取AndroidId */
    public String getAndroidId() {
        final String androidId = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
        String uuid;
        try
        {
            if (!"9774d56d682e549c".equals(androidId))
            {
                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();
            }
            else
            {
                final String deviceId = ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                uuid = (!TextUtils.isEmpty(deviceId)) ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString();
            }
        }
        catch (UnsupportedEncodingException e)
        {
            throw new RuntimeException(e);
        }
        return uuid;
    }

    /** 获取手机卡识别号IMSI */
    public String getImsi() {
        DeviceInfo deviceInfo = getDeviceInfo();
        if (deviceInfo == null) return "";
        if ("单卡芯片".equals(deviceInfo.chipName)) { return deviceInfo.imsi_1; }
        return deviceInfo.imsi_1 + "," + deviceInfo.imsi_2;
    }

    /** 获取手机识别号IMEI */
    public String getImei() {
        DeviceInfo deviceInfo = getDeviceInfo();
        if (deviceInfo == null) return "";
        if ("单卡芯片".equals(deviceInfo.chipName)) { return deviceInfo.imei_1; }
        return deviceInfo.imei_1 + "," + deviceInfo.imei_2;
    }

    /** 获取设备信息 */
    public DeviceInfo getDeviceInfo() {
        DeviceInfo deviceInfo = initQualcommDoubleSim();
        if (deviceInfo != null)
        {
            return deviceInfo;
        }
        else
        {
            deviceInfo = initMtkDoubleSim();
            if (deviceInfo != null)
            {
                return deviceInfo;
            }
            else
            {
                deviceInfo = initMtkSecondDoubleSim();
                if (deviceInfo != null)
                {
                    return deviceInfo;
                }
                else
                {
                    deviceInfo = initSpreadDoubleSim();
                    if (deviceInfo != null)
                    {
                        return deviceInfo;
                    }
                    else
                    {
                        deviceInfo = initSystemDevice();
                        if (deviceInfo != null)
                        {
                            return deviceInfo;
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
            }
        }
    }

    /** MTK的芯片的判断 */
    public DeviceInfo initMtkDoubleSim() {
        DeviceInfo deviceInfo;
        try
        {
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> c = Class.forName("com.android.internal.telephony.Phone");
            Field fields1 = c.getField("GEMINI_SIM_1");
            fields1.setAccessible(true);
            simId_1 = (Integer) fields1.get(null);
            Field fields2 = c.getField("GEMINI_SIM_2");
            fields2.setAccessible(true);
            simId_2 = (Integer) fields2.get(null);

            Method m = TelephonyManager.class.getDeclaredMethod("getSubscriberIdGemini", int.class);
            imsi_1 = (String) m.invoke(tm, simId_1);
            imsi_2 = (String) m.invoke(tm, simId_2);

            Method m1 = TelephonyManager.class.getDeclaredMethod("getDeviceIdGemini", int.class);
            imei_1 = (String) m1.invoke(tm, simId_1);
            imei_2 = (String) m1.invoke(tm, simId_2);

            deviceInfo = new DeviceInfo();
            deviceInfo.chipName = "MTK芯片";
            deviceInfo.imei_1 = imei_1;
            deviceInfo.imei_2 = imei_2;
            deviceInfo.imsi_1 = imsi_1;
            deviceInfo.imsi_2 = imsi_2;

        }
        catch (Exception e)
        {
            return null;
        }
        return deviceInfo;
    }

    /** MTK的芯片的判断2 */
    public DeviceInfo initMtkSecondDoubleSim() {
        DeviceInfo deviceInfo;
        try
        {
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> c = Class.forName("com.android.internal.telephony.Phone");
            Field fields1 = c.getField("GEMINI_SIM_1");
            fields1.setAccessible(true);
            simId_1 = (Integer) fields1.get(null);
            Field fields2 = c.getField("GEMINI_SIM_2");
            fields2.setAccessible(true);
            simId_2 = (Integer) fields2.get(null);

            Method mx = TelephonyManager.class.getMethod("getDefault", int.class);
            TelephonyManager tm1 = (TelephonyManager) mx.invoke(tm, simId_1);
            TelephonyManager tm2 = (TelephonyManager) mx.invoke(tm, simId_2);

            imsi_1 = tm1.getSubscriberId();
            imsi_2 = tm2.getSubscriberId();

            imei_1 = tm1.getDeviceId();
            imei_2 = tm2.getDeviceId();

            deviceInfo = new DeviceInfo();
            deviceInfo.chipName = "MTK芯片";
            deviceInfo.imei_1 = imei_1;
            deviceInfo.imei_2 = imei_2;
            deviceInfo.imsi_1 = imsi_1;
            deviceInfo.imsi_2 = imsi_2;

        }
        catch (Exception e)
        {
            return null;
        }
        return deviceInfo;
    }

    /** 展讯芯片的判断 */
    @SuppressWarnings ("WrongConstant")
    public DeviceInfo initSpreadDoubleSim() {
        DeviceInfo deviceInfo;
        try
        {
            Class<?> c = Class.forName("com.android.internal.telephony.PhoneFactory");
            Method m = c.getMethod("getServiceName", String.class, int.class);
            String spreadTmService = (String) m.invoke(c, Context.TELEPHONY_SERVICE, 1);
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            imsi_1 = tm.getSubscriberId();
            imei_1 = tm.getDeviceId();
            TelephonyManager tm1 = (TelephonyManager) mContext.getSystemService(spreadTmService);
            imsi_2 = tm1.getSubscriberId();
            imei_2 = tm1.getDeviceId();
            deviceInfo = new DeviceInfo();
            deviceInfo.chipName = "展讯芯片";
            deviceInfo.imei_1 = imei_1;
            deviceInfo.imei_2 = imei_2;
            deviceInfo.imsi_1 = imsi_1;
            deviceInfo.imsi_2 = imsi_2;
        }
        catch (Exception e)
        {
            return null;
        }
        return deviceInfo;
    }

    /** 高通芯片判断 */
    @SuppressWarnings ("WrongConstant")
    public DeviceInfo initQualcommDoubleSim() {
        DeviceInfo deviceInfo;
        try
        {
            Class<?> cx = Class.forName("android.telephony.MSimTelephonyManager");
            Object obj = mContext.getSystemService("phone_msim");
            Method md = cx.getMethod("getDeviceId", int.class);
            Method ms = cx.getMethod("getSubscriberId", int.class);
            imei_1 = (String) md.invoke(obj, simId_1);
            imei_2 = (String) md.invoke(obj, simId_2);
            imsi_1 = (String) ms.invoke(obj, simId_1);
            imsi_2 = (String) ms.invoke(obj, simId_2);
            Method mx = cx.getMethod("getPreferredDataSubscription", int.class);
            Method is = cx.getMethod("isMultiSimEnabled", int.class);
            int statePhoneType_2 = (Integer) mx.invoke(obj);
            boolean flag = (Boolean) is.invoke(obj);
            deviceInfo = new DeviceInfo();
            deviceInfo.chipName = "高通芯片-getPreferredDataSubscription:" + statePhoneType_2 + ",flag:" + flag;
            deviceInfo.imei_1 = imei_1;
            deviceInfo.imei_2 = imei_2;
            deviceInfo.imsi_1 = imsi_1;
            deviceInfo.imsi_2 = imsi_2;
        }
        catch (Exception e)
        {
            return null;
        }
        return deviceInfo;
    }

    /** 系统的api */
    public DeviceInfo initSystemDevice() {
        DeviceInfo deviceInfo;
        try
        {
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            imsi_1 = tm.getSubscriberId();
            imei_1 = tm.getDeviceId();
        }
        catch (Exception e)
        {
            return null;
        }
        if (TextUtils.isEmpty(imsi_1) || imsi_1.length() < 10)
        {
            return null;
        }
        else
        {
            deviceInfo = new DeviceInfo();
            deviceInfo.chipName = "单卡芯片";
            deviceInfo.imei_1 = imei_1;
            deviceInfo.imei_2 = "没有";
            deviceInfo.imsi_1 = imsi_1;
            deviceInfo.imsi_2 = "没有";
            return deviceInfo;
        }
    }

    public class DeviceInfo {
        public String chipName;
        public String imsi_1;
        public String imei_1;
        public String imsi_2;
        public String imei_2;
    }
}